<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters" class="demo-form-inline">
        <el-form-item label="广告名称">
          <el-input v-model="filters.keyword" placeholder="请输入广告名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryBaran">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="show(0)">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="contents" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="title" label="广告名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="categoryId" label="广告分类" width="160" sortable>
        <template slot-scope="scope">
          <span v-for="contentItem in contentItems" v-if="scope.row.categoryId==contentItem.id">{{contentItem.name}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="url" label="广告url" width="260" sortable>
      </el-table-column>
      <el-table-column prop="pic" label="广告图片" width="260" sortable>
        <template slot-scope="scope">
          <img width="100px" :src="scope.row.pic"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="广告状态" width="160" sortable>
        <template scope="scope">
          <span v-if="scope.row.status==='0'">禁用</span>
          <span v-if="scope.row.status==='1'">正常</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="show(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0" @click="batchRemove">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="showContents" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="广告名称" prop="title">
          <el-input v-model="showContents.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="广告状态" prop="status">
          <template>
            <el-radio v-model="showContents.status" label="1">正常</el-radio>
            <el-radio v-model="showContents.status" label="0">禁用</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="广告分类" prop="categoryId">
          <el-select v-model="showContents.categoryId" clearable placeholder="请选择">
            <el-option
                v-for="item in contentItems"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告url" prop="url">
          <el-input v-model="showContents.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="广告图片" prop="pic">
          <el-upload
              v-model="showContents.pic"
              class="upload-demo"
              action="http://localhost:10010/file/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="success"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import axios from "axios";
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
/*引入富文本编辑器集成文件上传服务器控件*/
import quillConfig from '@/assets/quill-config.js'
export default {
  data() {
    return {
      fileList: [
        {
          name: '',
          url: ''
        }
      ],
      ids: [],
      title: '',
      filters: {
        keyword: '',
        firstChar: ''
      },
      showContents: [{
        id: null,
        categoryId:null,
        title:'',
        url:'',
        pic:'',
        status:'',
        sortOrder:null
      }],
      contents: [],
      contentItems: [],
      total: 0,
      pageNum: 1,
      pageSize:5,
      listLoading: false,
      sels: [],//列表选中列
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

    }
  },
  methods: {

    success(response,file) {
      console.log(response);
      this.showContents.pic=response.data;
    },
    handleRemove() {

    },
    handlePreview(file,fileList) {

    },
    //查询全部广告类型
    queryContentItem(){
      axios.get("/contentItem/getAllContentItem").then(resp=>{
        console.log(resp);
        this.contentItems=resp.data.data;
      }).catch(resp=>{

      })
    },

    //获取品牌列表
    queryBaran(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum,   //页码
        trimParam:{
          name:this.filters.keyword,
          firstChar: this.filters.firstChar
        }
      }
      this.$http.post("/content/queryPage",params)
          .then(resp=>{
            console.log(resp);
              this.contents = resp.data.data.list;
              this.total = resp.data.data.total;
          }).catch(resp=>{
        alert("网络异常，稍后重试");
      })
    },
    //删除
   handleDel: function (index, row) {
     console.log(row);
     this.$confirm('确认删除该记录吗?', '温馨提示', {
       type: 'warning'
     }).then(() => {
       this.listLoading = true;
       this.$http.delete("/content/deleteById/"+row.id)
           .then(resp=>{
                 //去除忙等框
             if (resp.data.code==="1") {
               this.editFormVisible = false;
               this.$message({
                 message: '删除成功',
                 type: 'success'
               });
               this.listLoading = false;
               this.queryBaran();
             }
               }

           )
           .catch(result=>{
             //去除忙等框
             this.listLoading = false;
             //提示
             this.$message({
               message: '系统错误！',
               type: 'error'
             });
           })

     }).catch(() => {

     });
   },
   //显示编辑界面
   show (row) {
     console.log(row);
     this.editFormVisible = true;
     if (row === 0) {
       this.title = "新增";
       this.showContents = Object.assign({}, row);
       this.fileList=[{
         name: '',
         url: ''
       }];
     } else {
       this.title = "编辑";
       this.showContents = row;
       this.fileList[0].name = row.pic;
       this.fileList[0].url = row.pic;
     }
   },
   //编辑
   editSubmit: function () {
     this.$refs.editForm.validate((valid) => {
       if (valid) {
         this.$confirm('确认提交吗？', '提示', {}).then(() => {
           this.editLoading = true;
           //NProgress.start();
          axios.post("/content/save",this.showContents).then(resp=>{
            console.log(resp);
            if (resp.data.code==="1") {
              this.editFormVisible = false;
              this.$message({
                message: '保存成功',
                type: 'success'
              });
              this.editLoading = false;
              this.queryBaran();
            }
          }).catch(resp=>{

          })
         });
       }
     });
   },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.queryBaran();
    },

    selsChange: function (sels) {
      // console.log(sels);
      this.sels = sels;
    },
   //批量删除
   batchRemove: function () {
     // var ids = this.sels.map(item => item.id).toString();
     this.sels.forEach(item => this.ids.push(item.id));
     console.log(this.ids);
     this.$confirm('确认删除选中记录吗？', '提示', {
       type: 'warning'
     }).then(() => {
       this.listLoading = true;
       //NProgress.start();
       // let para = { ids: ids };
       this.$http.patch("/content/deleteBatch",this.ids).then((resp) => {
         if (resp.data.code==="1") {
           this.$message({
             message: '批量删除成功',
             type: 'success'
           });
           this.listLoading = false;
           this.queryBaran();
         }
       });
     }).catch(() => {

     });
   }
  },
  mounted() {
    this.queryBaran();
    this.queryContentItem();
  }
}

</script>

<style scoped>

</style>